<!-- 院外协同-经销商-后结算模块-财务管理-销售单列表 -->
<template>
  <div class="settlement-list-box">
    <el-container class="template-box">
      <!-- tab搜索 -->
      <tabs
        is-new-tabs
        :tabs-arr="indexTabsArr"
        @returnIndex="tabsHandle" />
      <!-- 搜索 -->
      <el-header height="auto">
        <SearchForm
          label-width="110px"
          :data="indexSearchData"
          :compact-data="indexSearchData.slice(0,3)"
          show-collapse
          @searchForm="searchForm"
          @resetForm="resetForm"
          :submit-loading='submitLoading'
        />
      </el-header>
      <!-- 列表 -->
      <el-editable
        v-loading="submitLoading"
        :data="dataList"
        height="400"
        :columns="indexTableData"
      >
        <!-- 操作 -->
        <template
          slot="default-action"
          slot-scope="scope">
          <el-button
            size="small"
            v-if="[12].includes(scope.row.orderStatus)"
            @click.stop="toOrderEdit(scope.row)"
            type="text text-primary">发货</el-button>
          <el-button
            size="small"
            v-if="[11].includes(scope.row.orderStatus)"
            @click.stop="toOrderEdit(scope.row)"
            type="text text-primary">响应</el-button>
          <el-button
            size="small"
            @click.stop="toOrderRead(scope.row)"
            type="text text-primary">详情</el-button>
        </template>
      </el-editable>
      <!-- 分页模块 -->
      <el-pagination
        class="pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="curPage"
        :page-sizes="PAGE_NUMBER"
        :page-size="pageSize"
        :layout="PAGINATION_LAYOUT"
        :total="totalElements"
      />
    </el-container>
  </div>
</template>

<script>
import { indexSearchData, indexTableData, indexTabsArr } from './const'
import { hasIn, cloneDeep } from 'lodash'
import SearchForm from '@components/businesscomponents/search'
import ElEditable from '@components/el-editable'
import { PAGE_NUMBER, PAGINATION_LAYOUT } from '@const/common'
import tabs from '@components/tabs/index.vue'
import {
  distributorSaleConfrim,
  distributorSaleDeliverDetail,
  distributorSaleList,
  distributorSaleOrderDetail,
  distributorSaleOrderOperate,
  distributorSaleCount,
  distributorSaleItemUpdate,
  distributorSaleWait
} from '@const/beforeSettleApi'
export default {
  components: { ElEditable, SearchForm, tabs },
  data () {
    return {
      changePath: `${this.$route.path}/change`,
      readPath: `${this.$route.path}/read`,
      // 搜索标题字段
      indexSearchData,
      // 搜索返回内容
      searchData: {},
      // 表格loading
      tableLoading: false,
      // 列表数据
      dataList: [],
      // 列表表头
      indexTableData,
      // tab字段
      indexTabsArr: cloneDeep(indexTabsArr),
      submitLoading: false,
      curPage: 1, // 当前页
      PAGE_NUMBER, // 页码数组
      PAGINATION_LAYOUT, // 页码功能布局
      pageSize: PAGE_NUMBER[0], // 一页的数量
      totalElements: 0, // 总共的条数
      tabIndex: 11
    }
  },
  computed: {

  },
  mounted () {
    this.getList()
    this.getNumber()
  },
  created () {
    // this.indexTabsArr[this.indexTabsArr.length - 1].badge.value = null
    this.indexTabsArr.forEach(item => {
      item.badge.value = null
    })
  },
  methods: {
    // 获取列表
    async getList () {
      this.searchData.settleBeginDate = this.searchData.settleBeginDate ? this.searchData.settleBeginDate.substring(0, 10) : ''
      this.searchData.settleEndDate = this.searchData.settleEndDate ? this.searchData.settleEndDate.substring(0, 10) : ''
      this.searchData.beginDate = this.searchData.beginDate ? this.searchData.beginDate.substring(0, 10) : ''
      this.searchData.endDate = this.searchData.endDate ? this.searchData.endDate.substring(0, 10) : ''
      this.submitLoading = true
      const res = await this.$axios({
        method: 'post',
        url: distributorSaleList,
        data: {
          ...this.searchData,
          orderStatus: this.tabIndex,
          page: this.curPage,
          pageSize: this.pageSize
        }
      })
      this.dataList = hasIn(res, ['data', 'payload'])
        ? res.data.payload.content || []
        : []
      this.totalElements = res.data.payload.totalElements
      this.submitLoading = false
    },
    // tabs切换处理事件
    tabsHandle (index) {
      this.tabIndex =
        this.indexTabsArr[index].val === 'null'
          ? null
          : this.indexTabsArr[index].val
      this.curPage = 1
      this.getList()
    },

    // 跳转详情
    toOrderRead (data) {
      // 已完成
      this.$router.push({
        path: this.readPath,
        query: {
          id: data.saleOrderId,
          orderStatus: data.orderStatus
        }
      })
    },
    // 去发货/响应
    toOrderEdit (data) {
      this.$router.push({
        path: this.changePath,
        query: {
          id: data.saleOrderId,
          orderStatus: data.orderStatus
        }
      })
    },
    // 获取订单数量
    async getNumber () {
      const res = await this.$axios({
        method: 'post',
        url: distributorSaleCount
      })
      let arr = hasIn(res, 'data.payload') ? res.data.payload : []
      if (!arr.length) {
        return
      }
      const cloneIndexTabs = cloneDeep(indexTabsArr)
      cloneIndexTabs.forEach(item => {
        arr.forEach(ite => {
          if (ite.statusCode === item.val) {
            item.badge = { value: ite.count }
          }
        })
        if (item.val === 0) {
          item.badge.value = 0
          arr.forEach(ite => {
            item.badge.value += ite.count
          })
        }
      })
      this.indexTabsArr = cloneIndexTabs
    },
    // 搜索功能-点击查询
    searchForm (condition) {
      this.searchData = condition
      this.getList()
    },

    // 点击重置
    resetForm (condition) {
      this.curPage = 1
      this.searchData = condition
    },
    // 页码更改
    handleSizeChange (size) {
      this.pageSize = size
      this.getList()
    },

    // 切换不一样的页面,重置一切数据
    handleCurrentChange (curPage) {
      this.curPage = curPage
      this.getList()
    }
  }
}
</script>

<style lang="less" scoped>
@import '../m-front-common/pc/assets/var.less';
.settlement-list-box {
  padding: 6px !important;
  background: @bgColor;
  .el-header {
    background: #fff;
    padding: 10px;
    margin-bottom: 16px;
  }
  .template-box {
    height: 100%;
    overflow: auto;
    .pagination {
      padding-top: 10px;
      display: flex;
      justify-content: flex-end;
      padding-right: 0;
    }
  }
}
</style>
